/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application.thread;

import application.ListOption;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.concurrent.Semaphore;

/**
 *
 * @author Simão
 */
public class ReaderThread extends Thread {

    private DataInputStream dataInputStream;
    private Semaphore readerSemaphore;
    private ProtocolThread serverThread;

    ReaderThread(DataInputStream dataInputStream, Semaphore readerSemaphore, ProtocolThread serverThread) {
        this.dataInputStream = dataInputStream;
        this.readerSemaphore = readerSemaphore;
        this.serverThread = serverThread;
    }

    @Override
    public void run() {
        byte[] stream;
        System.out.println("A new ReaderThread has stared.");
        while (true) {
            try {
                stream = new byte[dataInputStream.readInt()];
                dataInputStream.readFully(stream);
            } catch (IOException ex) {
                System.out.println("IOException in DataInputStream. Discarding stream.");
                continue;
            }
            serverThread.useReaderList(ListOption.ADD, stream);
            System.out.println("Message processed and added to ReaderList.");
        }
    }
}
